# -*- coding: utf-8 -*-
# @Author: Your name
# @Date:   2021-11-02 09:17:06
# @Last Modified by:   Your name
# @Last Modified time: 2021-12-02 14:48:07

import os
import time
import random
import PathConfig
import re

# 文件名替换 

fileNameList_old = []
fileNameList_new = []

#修改文件内容
def replaceFileContent(file_dir):
	fs = os.listdir(file_dir)
	for dir in fs:
		if dir.startswith('.'):
			#隐藏文件过滤隐藏
			continue

		tmp_path = os.path.join(file_dir, dir)
		if not os.path.isdir(tmp_path):

			with open(tmp_path) as f:
				file_str = f.read()

			if not dir.endswith('.h') and not dir.endswith('.m') and not dir.endswith('.xib') and not dir.endswith('.pch'):
				continue
		
			rOld = PathConfig.file_pre_class_type_old
			rNew = PathConfig.file_pre_class_type

			file_str = file_str.replace(rOld,rNew)
			file_str = file_str.replace(rOld.lower()+'_',rNew.lower()+'_') 
			file_str = file_str.replace(rOld.capitalize(),rNew.capitalize()) 

			with open(tmp_path, 'w') as f:
				f.write(file_str)

		else:
			# 是文件夹,则递归调用
			replaceFileContent(tmp_path)


# 替换文件夹名字
def replaceFileName(filepath, filename, oldName, newName):

# filepath:/Users/98du/Documents/98du/pros/NEDConstant/NEDConstant/Classes
# filename:YHConstant.m
# oldName:YH
# newName:NED 
	# print ('filepath:' + filepath + '\nfilename:' + filename + '\noldName:' + oldName + '\nnewName:' + newName)

	filepath_old = os.path.join(filepath, filename)

	filename_new = filename.replace(oldName,newName)

	filename_Old = filename.split('.')[0]
	fileType = filename.split('.')[1]
	
	if filename_Old not in fileNameList_old:

		fileNameList_old.append(filename_Old)		
		fileNameList_new.append(filename_new)


	filepath_new = os.path.join(filepath, filename_new)  
	
	print ('重命名文件：' + filename_Old + ' ==> ' + filename_new)

	inFile = open(filepath_old)
	outFile = open(filepath_new, 'w')
	for line in inFile:
		outFile.write(line)
	inFile.close()
	outFile.close()
	os.remove(filepath_old)
	

def traverse(file_dir):
    fs = os.listdir(file_dir)
    for dir in fs:
    	if dir.startswith('.'):
			#过滤隐藏
			continue

        tmp_path = os.path.join(file_dir, dir)
        if not os.path.isdir(tmp_path):
			if dir.startswith(PathConfig.file_pre_class_type_old):
				replaceFileName(file_dir, dir, PathConfig.file_pre_class_type_old, PathConfig.file_pre_class_type)
				
			elif dir.find('+'+PathConfig.file_pre_class_type_old) > 0 :
				replaceFileName(file_dir, dir, '+'+PathConfig.file_pre_class_type_old, '+'+PathConfig.file_pre_class_type)
        else:
            # 是文件夹,则递归调用
            traverse(tmp_path)



def main():

	traverse(PathConfig.rub_filepath)
  	replaceFileContent(PathConfig.rub_filepath)
	
	print (fileNameList_old)
	print (fileNameList_new)

	print('Finish done')

if __name__ == '__main__':
	main()





# for i in range(0,len(fileNameList_old)):
				

# PathConfig.file_pre_class_type_old, PathConfig.file_pre_class_type
				# oldName = fileNameList_old[i]
				# newName = fileNameList_new[i]

				# pattern = '\\b(%s)\\b' % oldName
				
				# file_str = re.sub(pattern,newName,file_str)
				
				# print(oldName + ' -----  ' + newName)

				# if oldName.find('+') > 1 and newName.find('+') > 1:
				# 	oldName = oldName.split('+')[1]
				# 	newName = newName.split('+')[1]

				# 	print (oldName + ' ==== ' + newName)

				# 	pattern = '\\b(%s)\\b' % oldName
				# 	file_str = re.sub(pattern,newName,file_str)


				# file_str = file_str.replace(oldName,newName)
				# file_str = file_str.replace(oldName.lower()+'_',newName.lower()+'_') 
				# file_str = file_str.replace(oldName.capitalize(),newName.capitalize()) 

				

			# print tmp_path
			# print ('替换文件内容  ' + tmp_path.split('/').pop())